home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 60.zip / BS1 part 60 / C mon v3 d3.adf / CMANV3_3.LHA / ACE9.lha / Devices / AudioDevice / AudioDevice.doc < prev    next >
Text File  |  1992-04-27  |  67KB  |  1,800 lines

  1. 4    AUDIO DEVICE
  2.  
  3. 4.1  INTRODUCTION
  4.  
  5. The Amiga was designed to be the first home computer which
  6. combined state of the art graphics and impressive sound
  7. capabilities. Although more than six years have passed since
  8. the first Amiga was released, very few other computers can
  9. compete with the sound quality and flexibility offered by the
  10. Amiga.
  11.  
  12. In this chapter we look at how you can use the sound system to
  13. produce sound effects as well as compose melodies. Since the
  14. sound system has been built into the rest of the operating
  15. system, it will work smoothly together with other processes, and
  16. can without problems be used by several tasks at the same time.
  17.  
  18.  
  19.  
  20. 4.1.1  SOUND
  21.  
  22. Sound is simply vibrating air that is registered by our ears
  23. and interpreted as lovely music, speech, white noise, etc...
  24. Although the simplicity it is possible to produce almost
  25. unlimited number of variations of even a simple tune. Even a
  26. single note sound very different if it was made by a piano or
  27. violin. By altering the volume and rate it is played it is
  28. possible to produce even more variations. 
  29.  
  30. To better understand what sound is and how it is produced we
  31. will first look at some commonly used technical words.
  32.  
  33. The "amplitude" of sound is the height of one of the waveforms.
  34. The higher amplitude the louder will the waveform sound.
  35.  
  36. The "frequency" is the number of waveforms produced each
  37. second. The higher frequency the higher will the tone be.
  38.  
  39. There exist a third sound variable called "timbre". The problem
  40. with it is that it is very hard to describe, but is still
  41. very important for how the sound will be perceived. It can
  42. be called "the sound identity". A violin and a piano can play
  43. a tone with the same amplitude and frequency, but it will
  44. still sound very different. Even two violins can sound very
  45. different.
  46.  
  47. The problem with reproducing the timbre is that by altering the
  48. frequency or amplitude just a little (does not even need to be
  49. a noticeable change) it can completely change the timbre. If you
  50. have read about "chaos theories" the timbre can be described
  51. with the "butterfly effect"; even a small change in wind by a
  52. butterfly can tip the balance and in the end cause a hurricane
  53. on the other side of the world.
  54.  
  55. By combining several frequencies each with its own amplitude
  56. and timbre we can produce unlimited numbers of variations.
  57.  
  58.  
  59.  
  60. 4.1.2  DIFFERENT WAVEFORMS
  61.  
  62. Although each waveform from a note is different from another
  63. we usually do not bother too much about it. It is usually
  64. enough to use just a few different elementary waveforms. By
  65. altering the frequency and amplitude of these waveforms we can
  66. then still produce millions of different sounds.
  67.  
  68. The most commonly used waveforms are the sine, triangular and
  69. square waveforms. See illustration "Waveforms".
  70.  
  71.  
  72.  
  73. 4.1.3  DIGITAL AND ANALOG WAVEFORMS
  74.  
  75. When we are using computers we can only handle digital numbers.
  76. To convert these digital numbers into sound we must first
  77. convert the digital numbers to analog values which can then
  78. be sent to a loud speaker. See illustration "AnalogDigital".
  79.  
  80. On each Amiga computer there exist four "sound channels" which
  81. each can convert digital numbers into analogue values. We can
  82. therefore play four different sounds at the same time, which
  83. means that very complex sounds can be produced.
  84.  
  85. Two of the sound channels are connected to the right audio
  86. port, and the other two are connected to the left audio port.
  87. We can therefore produce stereo sound on the Amiga.
  88.  
  89. The sound channels are sometimes called "eight-bit channels"
  90. because they convert eight-bit values into analog values. The
  91. Amiga can therefore produce sounds were the values in the
  92. waveforms ranges from -128 to 127. Compact Disk (CD) players
  93. use 16 bits and can therefore use even more values. For
  94. computers today eight bits are usually more than enough, and
  95. can produce very impressive sounds.
  96.  
  97.  
  98.  
  99. 4.1.4  PLAY SAMPLED SOUNDS OR CREATE YOUR OWN TUNES
  100.  
  101. On the Amiga you can create your own tunes by using a waveform
  102. and modulating it (changing the amplitude and frequency). You
  103. can also play sound data that has been sampled (converted from
  104. analog sound to digital data). By using sampled data you can
  105. reproduce very complex waveforms, and it will sound natural.
  106.  
  107. To load and use already sampled data can very easily be done
  108. with help of the two utilities I have included in the "Sound"
  109. manual, "Easy Sound" and "Include Sound".
  110.  
  111.  
  112.  
  113.  
  114.  
  115. 4.2  PREPARE THE AUDIO DEVICE
  116.  
  117. 4.2.1  PRIORITY
  118.  
  119. Since several programs may be running at the same time it may
  120. happen that two or more programs tries to use the same sound
  121. channel at the same time. To solve problems like this we have
  122. to use priorities.
  123.  
  124. If a program tries to use a sound channel that some other
  125. program is already using two things may happen. If the new
  126. program has the same or lower priority than the other program
  127. the new one is refused any access to the sound channel and the
  128. old program may continue to use it. On the other hand, if the
  129. new program has higher priority it will "steal" the sound
  130. channel from the old program.
  131.  
  132. Some sounds are more important than others. An emergency alarm
  133. is of course much more important than a background tune, and
  134. thus the emergency alarm should have higher priority. Commodore
  135. has suggested the following priorities:
  136.  
  137.   Type of sound             Suggested priority
  138.   --------------------------------------------
  139.   Unstoppable sound               127
  140.   Emergencies                      95
  141.   Attention                        85
  142.   Speech                           75
  143.   Information                      60
  144.   Music                             0
  145.   Sound effects                   -35
  146.   Background music                -90
  147.   Silence                        -128
  148.  
  149.  
  150. If a sound is so important that no one what ever happens should
  151. be able to interrupt it we should use the "unstoppable sound
  152. priority". You should never start a sound with this priority.
  153. You should instead start it with a lower (for example with the
  154. "emergency") priority, and then increase it.
  155.  
  156. The "emergency" priority should be used when something happens
  157. that is so important that the user must immediately know about
  158. it. Examples: the program is going to visit the guru if the
  159. user does not immediately do some thing. 
  160.  
  161. The "attention" priority is used to alert the user that
  162. something has happen and the user must as soon as possible know
  163. about it.
  164.  
  165. The "speech" priority is used by the "Narrator Device" when it
  166. is producing synthesized speech. Spoken information is usually
  167. more important for the user than a nice tune.
  168.  
  169. The "information" priority should be used when you want to tell
  170. the user something, but the user must not immediately know
  171. about it. This priority can be used when you want to tell the
  172. user to read something, or start to input some value etc...
  173.  
  174. When you are plying music you should use the "music" priority.
  175. This is for programs that are using music as one of the primary
  176. functions. Background tunes should use the "background
  177. priority".
  178.  
  179. The lowest priority level is "silent", and should actually never
  180. be used. If you do not want to play the sound you should free
  181. the sound channel instead.
  182.  
  183. The priority can be varied inside each priority group. For
  184. example; soft and quiet instruments/notes should have lower
  185. priority than loud and characteristic instruments/notes. You
  186. can also alter the priority while the sound is being played.
  187. The first part of an emergency sound can for example have
  188. higher priority than the rest of the sound.
  189.  
  190. Sadly there does not exist any header file that defines the
  191. different priorities. In my examples I will therefore use my
  192. own constants which are declared like this:
  193.  
  194.   Type of sound      Priority
  195.   ---------------------------
  196.   SOUND_UNSTOPPABLE    127
  197.   SOUND_EMERGENCIES     95
  198.   SOUND_ATTENTION       85
  199.   SOUND_SPEECH          75
  200.   SOUND_INFORMATION     60
  201.   SOUND_MUSIC            0
  202.   SOUND_EFFECT         -35
  203.   SOUND_BACKGROUND     -90
  204.   SOUND_SILENCE       -128
  205.  
  206.  
  207.  
  208. 4.2.2  ALLOCATING CHANNELS
  209.  
  210. There exist four sound channels which each one can convert
  211. digital sound data into analogue sound which is sent to a
  212. loudspeaker. The first and last sound channel is connected to
  213. the left audio port, while the second and third is connected to
  214. the right audio port.
  215.  
  216. When you are reserving a sound channel you specify which
  217. channels you want to use by using a value where the first bit
  218. represents the first sound channel, the second bit the second
  219. sound channel and so on...
  220.  
  221.  
  222.   Bit:         3     2      1      0
  223.   -------------------------------------
  224.   Value:       8     4      2      1
  225.   Channel:     3     2      1      0
  226.   Audio port:  Left  Right  Right  Left
  227.  
  228. To reserve the first and last channel (both using the left
  229. sound channel) you would use the value 9 (1001[bin] = 9[dec]).
  230. To reserve the second and third channel (both using the right
  231. sound channel) you would use the value 6 (0110[bin] = 6[dec]).
  232. To reserve all four sound channels set the value to 15
  233. (1111[bin] = 15[dec]).
  234.  
  235. Since other programs may be running at the same time it may
  236. happen that they are already using one or more of the sound
  237. channels. If they have lower priority than your program, you
  238. will "steal" the sound channel from them. However, if you have
  239. the same or lower priority, you are refused any access to that
  240. sound channel. 
  241.  
  242. The good thing is that you may use an array of allocation
  243. values, and if the first value could not be satisfied the
  244. audio device will automatically try the next value. If you are
  245. lucky there exist a combination of sound channels which is not
  246. currently being used.
  247.  
  248. If you want to play some sounds in stereo you have to use one
  249. sound channel from the right and one from the left port. There
  250. exist four possible combinations which satisfy this, and the
  251. "allocation array" should therefore look like this:
  252.  
  253.   Dec    Bin     Description
  254.   ---------------------------------------------------
  255.    3    0011    First left and first right channel.
  256.    5    0101    First left and second right channel.
  257.   10    1010    Second left and first right channel.
  258.   12    1100    Second left and second right channel.
  259.  
  260.   UBYTE allocation_array = { 3, 5, 10,12 };
  261.  
  262.  
  263. The audio device will first try all combinations in the
  264. allocation array before it will use the priority to "steal"
  265. the channels for you.
  266.  
  267.  
  268.  
  269. 4.2.3  CREATE WAVEFORMS
  270.  
  271. The audio device can play any type of waveform that is ranging
  272. from -128 up to 127. Usually you will use the normal sine,
  273. triangle, and square wave forms, but you can equally well play
  274. more complex waveforms and even directly sample sound.
  275.  
  276. It is important to note that the audio device is a part of the
  277. special custom chips in the Amiga and can therefore only access
  278. sound data which is located in the "chip memory". The data must
  279. also start on a word boundary, and consists of an even number
  280. of bytes.
  281.  
  282. The more data used for the waveform the more complex waves can
  283. be used. The smallest waveforms consists of only two values.
  284.  
  285.   Example: We will create a sin waveform with 16 values.
  286.  
  287.   /* If we are using mathematical functions */
  288.   /* like sin() we must include this file:  */
  289.   #include <math.h>
  290.  
  291.   /* Define a constant for the number of values in */
  292.   /* the wave form. It can then easily be changed. */
  293.   #define SINE_DATA_LENGTH 16
  294.  
  295.   /* Declare a pointer to  our sine wave: */
  296.   BYTE *sine_wave;
  297.  
  298.   /* ... */
  299.  
  300.   /* Allocate some chip memory for the sine wave: */
  301.   /* (All memory allocated by AllocMem() will     */
  302.   /* always start on a word boundary.)            */
  303.   sine_wave = (BYTE *) AllocMem( SINE_DATA_LENGTH, MEMF_CHIP );
  304.  
  305.   /* Have we got the memory? */
  306.   if( !sine_wave )
  307.     clean_up( "Not enough memory!" );  
  308.  
  309.   /* Initialize the sine waveform: */
  310.   for( loop = 0; loop < SINE_DATA_LENGTH; loop++ )
  311.     sine_wave[ loop ] =
  312.       127 * sin( loop * 2 * PI / SINE_DATA_LENGTH );
  313.  
  314.  
  315.        
  316. 4.2.4  NOTES AND FREQUENCIES
  317.  
  318. On octave consists of 12 notes. Here is a list of the
  319. frequencies that represents the notes which are one octave
  320. higher than the middle octave on a piano:
  321.  
  322.   Note  Frequency
  323.   ---------------
  324.    A      880.0
  325.    A#     932.3
  326.    B      987.8
  327.    C     1046.5
  328.    C#    1108.7
  329.    D     1174.7
  330.    D#    1244.5
  331.    E     1318.5
  332.    F     1396.9
  333.    F#    1480.0
  334.    G     1568.0
  335.    G#    1661.2 
  336.  
  337. To change octave you simply double/half these values. When you
  338. double the values you go one octave up, and when you half the
  339. values you go one octave down. Example, A = 880, one octave
  340. lower A = 440, one octave higher A = 1760, two octaves higher
  341. and A = 3520.
  342.  
  343. When the audio device should play a waveform you do not specify
  344. which frequency should be used. Instead you specify the
  345. "period" value which should be used for each sample (value) in
  346. the waveform. The period can be calculated with the following
  347. formula:
  348.  
  349.   Period = Clock ticks per second / Frequency / Number of samples 
  350.   
  351. The speed of the clock depends on if you have an European (PAL)
  352. or American (NTSC) Amiga. On an NTSC machine the clock ticks
  353. 3579545 times per second, while on a PAL machine it only ticks
  354. 3546895 times per second.
  355.  
  356.   Example: We want to play the sine waveform we previously
  357.   made with the frequency 880 (note A). (We are using an
  358.   European PAL Amiga.)
  359.   
  360.   period = 3546895 / 880 / SINE_DATA_LENGTH;
  361.  
  362.  
  363. Some extra information: If you have not already noticed it you
  364. may get the wrong answer when you are dividing values with each
  365. other. The problem is that C is always using the most accurate
  366. value in the formula as the base on which it does the
  367. calculations. If you multiply an integer value and a float value
  368. the C compiler will do the intermediate calculations with float
  369. values. If you on the other hand multiply two integers with
  370. each other, the C compiler will do the intermediate calculations
  371. with integer values.
  372.  
  373. The problem is that if you divide two values with each other,
  374. and both are integers, the calculations will be preformed with
  375. integer values. Since you do not have any decimals you can end
  376. up with a completely wrong answer. With multiplications (and
  377. addition and subtraction as well) this will never cause any
  378. errors.
  379.  
  380. When you are dividing two values with each other you should
  381. therefore always convert the value (which you divide the other
  382. value with) to a float (or double if necessary). Simply use
  383. normal casting. The rule is to always put the word "(float)"
  384. in front of the bottom value when you are using division. In
  385. our example the formula should look like this: (The second
  386. float is actually unnecessary, but it does not matter.)
  387.  
  388.   period = 3546895 / (float) 880 / (float) SINE_DATA_LENGTH;
  389.  
  390.  
  391.  
  392. 4.2.5  THE AUDIO REQUEST BLOCK
  393.  
  394. The audio device is controlled like any other device with help
  395. of request blocks. When you are using the audio device you
  396. should use the extended "IOAudio" structure which is declared
  397. in the header file "devices/audio.h" as this:
  398.  
  399. struct IOAudio
  400. {
  401.   struct IORequest ioa_Request;
  402.   WORD ioa_AllocKey;
  403.   UBYTE *ioa_Data;
  404.   ULONG ioa_Length;
  405.   UWORD ioa_Period;
  406.   UWORD ioa_Volume;
  407.   UWORD ioa_Cycles;
  408.   struct Message ioa_WriteMsg;
  409. };
  410.  
  411. ioa_Request:  The top part of the request block consists as
  412.               always of an IORequest structure. See below
  413.               for more information about this structure.
  414.               
  415.  
  416. ioa_AllocKey: When you reserve a sound channel this field is
  417.               given an unique "key" value. Each time you pass
  418.               the request to the channel this value will be
  419.               compared with the channel's current key value.
  420.               If these two values are not the same, the channel
  421.               has been stolen by some other program with higher
  422.               priority, and your command is returned with the
  423.               AUDIO_NOALLOCATION flag set. 
  424.  
  425.               If you are going to use another request block,
  426.               and not the one you used to allocate the channel
  427.               with, you have to copy this key value to the
  428.               other request block.
  429.  
  430. ioa_Data:     Pointer to the waveform data. Note that the data
  431.               must be in chip memory, and start on a word
  432.               boundary.
  433.  
  434. ioa_Length:   If you are going to play a waveform this filed
  435.               should be given the length (number of samples/
  436.               values in the wave data).
  437.  
  438. ioa_Period:   This field should be given a period value. The
  439.               period value is the number of clock ticks that
  440.               should be used for each sample (value in the
  441.               waveform). See above for more information about
  442.               how to calculate the period value. 
  443.  
  444. ioa_Volume:   The volume, which should be set to any value
  445.               between 0 and 64. The maximum value is 64, and
  446.               the minimum value is 0 (silent). Many programs
  447.               are always using the maximum volume, but if you
  448.               use different volume for different situations
  449.               you will gain a lot of "depth".
  450.               
  451.               Imagine an adventure game where most of the sound
  452.               effects are played rather softly. The user will
  453.               then use a higher volume on his/her own stereo
  454.               (or whatever the sound ports are connected to) to
  455.               compensate for the low volume. No imagine the
  456.               feeling when a monster appear and you at the same
  457.               time suddenly increase the volume to maximum!
  458.  
  459.               Sadly very few programs are using this very
  460.               effective technique.
  461.  
  462. ioa_Cycles:   Before you play a waveform you should set this
  463.               field to the number of times the waveform should
  464.               be played. If you set this field to 0 the
  465.               waveform will continuously be played until you
  466.               aborts the command.
  467.  
  468. ioa_WriteMsg: If you have set the flag "ADIO_WRITEMESSAGE" in
  469.               the "io_Flag" field, and this request will start
  470.               a sound, this message will be sent when the sound
  471.               starts to be played. Remember to give the
  472.               "mn_ReplyPort" field of the Message structure a
  473.               pointer to a message port to which the message
  474.               should be sent.
  475.  
  476.  
  477. The IORequest structure (which is a part of the IOAudio
  478. structure) is defined in the "exec/io.h" header file like this:
  479.  
  480. struct IORequest
  481. {
  482.   struct Message io_Message;
  483.   struct Device  *io_Device;
  484.   struct Unit    *io_Unit;
  485.   UWORD  io_Command;
  486.   UBYTE  io_Flags;
  487.   BYTE   io_Error;
  488. };
  489.  
  490. io_Message: The top part consists of a Message structure which
  491.             will be sent to us when the request has been
  492.             completed (successfully or not).
  493.             
  494.             The Message structure is defined in the header file
  495.             "exec/ports.h" like this:
  496.             
  497.             struct Message
  498.             {
  499.               struct Node mn_Node;
  500.               struct MsgPort *mn_ReplyPort;
  501.               UWORD mn_Length;
  502.             };
  503.  
  504.             mn_Node: It is only one field of the Node structure
  505.                      that should be used, and it is the "ln_Pri"
  506.                      filed, which should be given this sound's
  507.                      priority value.
  508.              
  509.             mn_ReplyPort: This field should be given a pointer
  510.                           to the message port the reply should
  511.                           be sent to.
  512.             
  513.             mn_Length:    This value can be ignored.
  514.  
  515. io_Device:  This field will automatically be initialized when
  516.             you use the request block together with an
  517.             OpenDevice() function call. It is simply a pointer
  518.             to the device which this request block is made for.
  519.  
  520. io_Unit:    This field will automatically be initialized when
  521.             you reserve an audio channel for this request
  522.             block.
  523.  
  524. io_Command: All commands should be set here. The following
  525.             commands are accepted by the audio device, and may
  526.             be used: (Will be explained below.)
  527.  
  528.             The unique audio device commands:
  529.  
  530.               ADCMD_FREE      Free a sound channel.
  531.               ADCMD_SETPREC   Set priority.
  532.               ADCMD_FINISH    Stop playing the sound.
  533.               ADCMD_PERVOL    Set period and volume.
  534.               ADCMD_LOCK      Lock a sound channel.
  535.               ADCMD_WAITCYCLE Wait for the cycle to finish.
  536.               ADCMD_ALLOCATE  Reserve a sound channel.
  537.  
  538.             The normal commands which are accepted by most of
  539.             the devices:
  540.  
  541.               CMD_WRITE       Play the waveform.
  542.               CMD_READ        Get a pointer to current req.
  543.               CMD_STOP        Stop and do not start any sound.
  544.               CMD_START       Start to play sound again.
  545.               CMD_FLUSH       Remove all queued requests.
  546.               CMD_RESET       Reset the audio device.
  547.  
  548. io_Flags:   The following flags may be used:
  549.  
  550.               IOF_QUICK          If this flag is set no messages
  551.                                  will be sent to the reply port.
  552.                                  This will speed up the
  553.                                  execution, and can be useful if
  554.                                  you have to use a lot of
  555.                                  request blocks.
  556.  
  557.               ADIOF_PERVOL       When you start to play a sound
  558.                                  you can either use the volume
  559.                                  and period values which were
  560.                                  used last time, or you may use
  561.                                  new values. If you want to use
  562.                                  new volume and period values
  563.                                  you should set this flag, and
  564.                                  also set the new values in
  565.                                  this request block. Otherwise
  566.                                  the old values will be used.
  567.  
  568.               ADIOF_SYNCCYCLE    Set this flag if you want that
  569.                                  the command should be
  570.                                  synchronized with the wave form.
  571.                                  If this flag is set, any
  572.                                  modifications will first occur
  573.                                  when the whole waveform (cycle)
  574.                                  have been played.
  575.  
  576.               ADIOF_NOWAIT       When you try to reserve a sound
  577.                                  channel (with ADCMD_ALLOCATE)
  578.                                  and the audio device can not
  579.                                  find any channel for you, it
  580.                                  will continue to try until it
  581.                                  succeeds. The request will
  582.                                  firs be returned when the
  583.                                  request has successfully been
  584.                                  completed.
  585.                                  
  586.                                  If you do not want to wait for
  587.                                  the request to be completed
  588.                                  you can set this flag. If the
  589.                                  audio device can not find any
  590.                                  sound channel it will
  591.                                  immediately return the request
  592.                                  with the "ADIOERR_ALLOCFAILED"
  593.                                  error flag set. 
  594.  
  595.               ADIOF_WRITEMESSAGE Normally you will only receive
  596.                                  a message at your reply port
  597.                                  when a request has been
  598.                                  completed. It may however
  599.                                  sometimes be necessary to know
  600.                                  when an audio request have
  601.                                  started.
  602.                                  
  603.                                  If you set this flag the
  604.                                  message at the end of the
  605.                                  request ("ioa_WriteMsg") will
  606.                                  be sent. Remember to give the
  607.                                  message structure a pointer to
  608.                                  a reply port if you are using
  609.                                  this flag.
  610.  
  611. io_Error:   If the request can not successfully be executed by
  612.             the audio device, it will be returned with one of
  613.             the following error flags:
  614.  
  615.               ADIOERR_NOALLOCATION  When you reserve a sound
  616.                                     channel you are given a
  617.                                     "key" value (set in the
  618.                                     "ioa_AllocKey" field).
  619.                                     Every time you send a
  620.                                     request this key value will
  621.                                     be compared to the sound
  622.                                     channel's current key
  623.                                     value. If these two values
  624.                                     do not match the channel
  625.                                     have been "stolen" by some
  626.                                     other program. The request
  627.                                     will then be returned with
  628.                                     this error flag set.
  629.                                     
  630.                                     Note that you do not have
  631.                                     to free a sound channel
  632.                                     that has been stolen from
  633.                                     you.
  634.  
  635.  
  636.               ADIOERR_ALLOCFAILED   When you try to reserve a
  637.                                     sound channel it may happen
  638.                                     that all desired channels
  639.                                     are occupied. If the audio
  640.                                     device can not find a
  641.                                     channel it will normally
  642.                                     continue to try until it
  643.                                     found one. However, if you
  644.                                     have set the "ADIOF_NOWAIT"
  645.                                     flag the request will
  646.                                     immediately be returned if
  647.                                     no sound channels could be
  648.                                     allocated, and this error
  649.                                     flag is then set.
  650.                                     
  651.                                     Remember to always check if
  652.                                     you have received the
  653.                                     channel or not if you are
  654.                                     using the "ADIOF_NOWAIT"
  655.                                     flag. 
  656.  
  657.               ADIOERR_CHANNELSTOLEN If you have "locked" a
  658.                                     channel, and another
  659.                                     program tries to steal
  660.                                     it, the lock request will
  661.                                     be returned to you with
  662.                                     this flag set.
  663.                                     
  664.                                     If the lock request is
  665.                                     returned to you with this
  666.                                     flag set you should as
  667.                                     fast as possible clean up
  668.                                     after you and unlock the
  669.                                     channel. It is first when
  670.                                     you free the locked channel
  671.                                     the other program gets it.
  672.  
  673.                                     NOTE! You must free the
  674.                                     channel as soon as
  675.                                     possible! The other program
  676.                                     has higher priority, but
  677.                                     since you have locked the
  678.                                     channel it can not get it.
  679.  
  680.  
  681.  
  682. 4.2.6  OPEN THE AUDIO DEVICE  
  683.  
  684. The audio device is very similar to other devices. You open
  685. a message port through which the audio device can reply to
  686. you. You then allocate the request block(s), and finally open
  687. the device.
  688.  
  689.   1. Open a message port. Since it is only our task and the
  690.      audio device that will use the message port, we do not
  691.      need to make it "public", therefore no name. Priority
  692.      should as usual be set to 0, normal priority. (Do not
  693.      mix up this priority for the message port with the
  694.      priority for the sound channel, which will later be set
  695.      in the request block.)
  696.  
  697.        struct MsgPort *replymp;
  698.  
  699.        replymp = (struct MsgPort *)
  700.          CreatePort( NULL, 0 );
  701.  
  702.        if( !replymp )
  703.          clean_up( "Could not create the reply port!" );
  704.  
  705.  
  706.   2. Allocate a request block of type IOAudio structure.
  707.      The IOAudio structure is an extended version of the
  708.      normal request block, and should therefore be allocated
  709.      with help of the CreateExtIO() function with the size
  710.      set to sizeof( struct IOAudio ).
  711.  
  712.        struct IOAudio *audio_req;
  713.  
  714.        audio_req = (struct IOAudio *)
  715.          CreateExtIO( replymp, sizeof( struct IOAudio ) );
  716.  
  717.        if( !audio_req )
  718.          clean_up( "Not enough memory!" );
  719.  
  720.  
  721.   3. Once the message port and the request block have
  722.      successfully been created you can "open" (gain access to)
  723.      the audio device.
  724.  
  725.        error = OpenDevice( AUDIONAME, 0, audio_req, 0 );
  726.  
  727.        if( error )
  728.          clean_up( "Could not open the Audio Device!" );
  729.  
  730.  
  731.      If you want you can reserve sound channels at the same
  732.      time as you open the device. You must then:
  733.  
  734.      (a) Set the sound priority. The top part of the request
  735.          block, the IORequest structure (ioa_Request) contains
  736.          a Message structure ("io_Message") which contains a
  737.          Node structure (mn_Node) which finally contains a
  738.          "ln_Pri" field. (Piece of cake.) It is in this field
  739.          you set the sound priority.
  740.  
  741.      (b) Give the "ioa_Data" field a pointer to an array were
  742.          the desired channel combinations are listed. This
  743.          array of desired channels are usually called "the
  744.          allocation array".
  745.          
  746.      (c) Set the length of the allocation array in the
  747.          "ioa_Length" field.
  748.          
  749.      If the "ioa_Length" is not zero when you try to open the
  750.      device, the audio device will also try to reserve the
  751.      desired channel(s). If the audio device can not find any
  752.      sound channels free, it will immediately (regardless if
  753.      you have set the "ADIOF_NOWAIT" flag or not) return with
  754.      the "ADIOERR_ALLOCFAILED" error message. 
  755.       
  756.  
  757.  
  758. 4.2.7  RESERVE CHANNELS
  759.  
  760. If you have not reserved any channel(s) when you opened the
  761. audio device, or you want to reserve even more channels, you
  762. should use the "ADCMD_ALLOCATE" command. The follwing things
  763. must be done:
  764.  
  765.   1. Set the "ADCMD_ALLOCATE" flag in the "io_Command" field
  766.      of the IORequest structure.
  767.  
  768.        audio_req->ioa_Request.io_Command = ADCMD_ALLOCATE;
  769.  
  770.   2. Set the sound priority. The top part of the request block,
  771.      the IORequest structure (ioa_Request) contains a Message
  772.      structure ("io_Message") which contains a Node structure
  773.      (mn_Node) which finally contains a "ln_Pri" field. It is
  774.      in this field you set the sound priority. Be careful so
  775.      you set the correct sound priority. 
  776.  
  777.        audio_req->ioa_Request.io_Message.mn_Node.ln_Pri =
  778.          SOUND_EFFECT;
  779.  
  780.   3. Normally will the request first be returned when the
  781.      audio device has successfully reserved the sound
  782.      channel(s). If you set the "ADIOF_NOWAIT" flag in the
  783.      "io_Flags" field of the IORequest structure, the request
  784.      will immediately be returned, successfully or not. You can
  785.      examine the "io_Error" field of the IORequest structure to
  786.      see if the request was successful or not. If no channels
  787.      could be reserved (allocated) the "ADIOERR_ALLOCFAILED"
  788.      error flag is set.
  789.  
  790.        audio_req->ioa_Request.io_Flags = ADIOF_NOWAIT;
  791.  
  792.   4. Give the "ioa_Data" field a pointer to an array were
  793.      the desired channel combinations are listed.
  794.  
  795.        audio_req->ioa_Data = allocation_array;
  796.   
  797.   5. Set the length of the allocation array in the
  798.      "ioa_Length" field.
  799.  
  800.        audio_req->ioa_Length = sizeof( allocation_array );
  801.          
  802.   6. The audio request block has now been properly initialized,
  803.      and it should now be sent to the audio device. Normally
  804.      you use the DoIO() and SendIO() commands, but they may not
  805.      be used when you are allocating channels. Instead you must
  806.      use the low level function BeginIO(), which is very
  807.      similar to SendIO().
  808.      
  809.      The reason why you may not use the DoIO() and SendIO() is
  810.      that they will erase some parts of the request block that
  811.      may not be altered when you are using the audio device.
  812.      
  813.      Since BeginIO() is an asynchronous command, it will
  814.      immediately return the control to the program, you have to
  815.      wait for the request to be executed. Simply use the
  816.      WaitIO() function which will put our program to sleep
  817.      while we are waiting.
  818.      
  819.      WaitIO() will return 0 when the request has successfully
  820.      been executed, else a non zero value is returned.
  821.  
  822.        BeginIO( audio_req );
  823.  
  824.        error = WaitIO( audio_req );
  825.  
  826.        if( error )
  827.          clean_up( "No channels!" );
  828.  
  829.  
  830.      After you have successfully allocated a channel you can
  831.      check which channel(s) you received by looking at the
  832.      "io_Unit" field of the IORequest structure. (Note that
  833.      normally the "io_Unit" field of a IORequest structure
  834.      contains a pointer to a Unit structure. However, when
  835.      you are using the audio device this field is used to store
  836.      an integer value, where the first four bits are used to
  837.      identify which channels are reserved. As before, bit zero
  838.      represents channel 0 (left), bit one channel 1 (right),
  839.      bit two channel 2 (right) and finally bit three represents
  840.      channel 3 (left).
  841.  
  842.        channel = (UBYTE) audio_req->ioa_Request.io_Unit;
  843.  
  844.        if( channel & 1 )
  845.          printf( "First left channel!\n" );
  846.  
  847.        if( channel & 2 )
  848.          printf( "First right channel!\n" );
  849.  
  850.        if( channel & 4 )
  851.          printf( "Second right channel!\n" );
  852.  
  853.        if( channel & 8 )
  854.          printf( "Second left channel!\n" );
  855.  
  856.  
  857.  
  858. 4.2.8  LOCK CHANNELS
  859.  
  860. After you have reserved a channel you may start to use it. When
  861. you reserved the sound channel was the "ioa_AllocKey" given an
  862. unique key number. This key number will be compared with the
  863. sound channels current key number, and if they do not match,
  864. the channel(s) has/have been stolen and your request is
  865. returned with the error flag "ADIOERR_NOALLOCATION" set.
  866.  
  867. If you want to use several request blocks for the same 
  868. channel(s) you must therefore copy the key value to all request
  869. blocks.
  870.  
  871. When you are using the request block to send commands the key
  872. value will always be checked before any change is made. It can
  873. however sometimes be necessary to skip the request block and
  874. directly modify the hardware registers. Using request blocks
  875. take some time, and if you have to make changes several times
  876. each second it may not be fast enough. You must then directly
  877. modify the sound registers.
  878.  
  879. Normally you are not allowed to hit the hardware like this, but
  880. when you are using the audio device you are allowed to do it.
  881. Of course, if you can manage with the request blocks you should
  882. use them.
  883.  
  884. If you are using the hardware registers you may get into
  885. trouble if you are not careful. If some other program has
  886. stolen the channel(s) from you and you use the hardware
  887. registers, you will not receive any error messages. You can now
  888. end up with modifying the other programs sounds, and this is
  889. not to be recommended!
  890.  
  891. Before you may use the hardware registers you must therefore
  892. "lock" the channel(s). To lock a channel you have to use a
  893. separate request block. This request block is allocated and
  894. pre-initialized as usual with help of the CreateExtIO()
  895. function: (We can of course use a different message port if
  896. you want to separate all lock messages, but it is usually
  897. easier to use only one message port for all audio messages.)
  898.  
  899.   struct IOAudio *audio_lock;
  900.  
  901.   audio_lock = (struct IOAudio *)
  902.     CreateExtIO( replymp, sizeof( struct IOAudio ) );
  903.  
  904.   if( !audio_lock )
  905.     clean_up( "Not enough memory!" );
  906.  
  907.  
  908. The lock should then have the following fields set:
  909.  
  910.   1. The "ADCMD_LOCK" flag in the io_Command field of the
  911.      IORequest structure must be set.
  912.  
  913.       audio_lock->ioa_Request.io_Command = ADCMD_LOCK;
  914.  
  915.   2. The lock must be linked to the already opened audio
  916.      device. The address to the device can be found in the
  917.      "io_Device" field of the IORequest structure.
  918.  
  919.       audio_lock->ioa_Request.io_Device =
  920.         audio_req->ioa_Request.io_Device;
  921.  
  922.   3. You must tell the device which channels you want to
  923.      lock. Normally you will try to lock the channels you
  924.      previously have reserved, and thus you can find the
  925.      channel value in the "io_Unit" field of the IORequest
  926.      block.
  927.  
  928.       audio_lock->ioa_Request.io_Unit =
  929.         audio_req->ioa_Request.io_Unit;
  930.  
  931.   4. Since you are going to use a new request block for some
  932.      channels you have previously reserved with another request
  933.      block, you must copy the key value:
  934.  
  935.      audio_lock->ioa_AllocKey = audio_req->ioa_AllocKey;
  936.  
  937.  
  938. Once the lock request has been initialized it should be send
  939. by calling the BeginIO() function.
  940.  
  941.   BeginIO( audio_lock );
  942.  
  943.  
  944. Once you have lock the channel(s) the request block will first
  945. be sent back when some other programs tries to steal the sound
  946. channel(s) or when you free the channel(s). If some other
  947. program wants the channel(s) and has higher priority, the lock
  948. will be returned with the error flag "ADIOERR_CHANNELSTOLEN"
  949. set. The channel has not yet been stolen, but you MUST
  950. immediately clean up all necessary things and free the
  951. channel(s) as soon as possible so the other program can get
  952. them. You may NOT keep the channels once the lock has been
  953. returned!
  954.  
  955. While you are cleaning up and freeing the channels the other
  956. program is put to sleep and does not know what is happening.
  957. If you are not fast enough the other program will continue to
  958. "sleep" although it has higher priority and should therefore
  959. immediately get the channels if you have not locked them. You
  960. must therefore free the channels as fast as possible.
  961.  
  962. You may of course not modify the hardware registers any more
  963. after you have deallocated the audio channel(s).
  964.  
  965. You can use the function CheckIO() to see if the lock has been
  966. sent back. See below for more information about how to free a
  967. sound channel.
  968.  
  969.   /* Has the lock been sent back? */
  970.   if( CheckIO( audio_lock ) )
  971.   {
  972.     /* Another programs what our channel(s)! */
  973.  
  974.     /* Clean up. Reset any hardware */
  975.     /* registers etc, if necessary. */
  976.  
  977.     /* Free the channel: */
  978.     audio_req->ioa_Request.io_Command = ADCMD_FREE;
  979.  
  980.     /* Send the request: (We may use the DoIO() function */
  981.     /* when we deallocate a channel.)                    */
  982.     DoIO( audio_req );
  983.   }
  984.  
  985.  
  986.  
  987. 4.3  USE THE AUDIO DEVICE
  988.  
  989. Once you have declared a request block and successfully opened
  990. the audio device you may start to use it. When you are playing
  991. sounds you may either use the request block to issue the sound
  992. commands, or you can directly modify the hardware registers.
  993. The first method is cleaner, but the second method is much
  994. faster and may sometimes be necessary if you intend to issue
  995. many commands.
  996.  
  997.  
  998. 4.3.1  PLAY SOUNDS
  999.  
  1000. To play some sound you have to do the following things:
  1001.  
  1002.   1. Give the "ioa_Data" field of the request block a pointer to
  1003.      a waveform you want to play. (The waveform data must be
  1004.      located in chip memory, and start on an even byte address.)
  1005.  
  1006.        audio_req->ioa_Data = sine_wave;
  1007.  
  1008.   2. Set the length of the waveform in the "ioa_Length" field.
  1009.      (Must be an even number of bytes.)
  1010.  
  1011.        audio_req->ioa_Length = SINE_DATA_LENGTH;
  1012.  
  1013.   3. Set the number of times (cycles) the waveform should be
  1014.      played in the "ioa_Cycles" field. If you want to play the
  1015.      waveform continuously until you abort the request you
  1016.      should set the field to 0.
  1017.  
  1018.        audio_req->ioa_Cycles = 3;
  1019.  
  1020.   4. Set the command "CMD_WRITE" flag in the "io_Command" field
  1021.      which tells the device that you want to play a tune.
  1022.  
  1023.        audio_req->ioa_Request.io_Command = CMD_WRITE;
  1024.  
  1025.   5. If you want to use a specific volume and period values you
  1026.      must set the "ADIOF_PERVOL" flag in the "io_Flags" field. 
  1027.      The values in the "ioa_Volume" and "ioa_Period" fields
  1028.      will then be used, else the previous volume and period
  1029.      values will be used.
  1030.      
  1031.        audio_req->ioa_Request.io_Flags = ADIOF_PERVOL;
  1032.        audio_req->ioa_Volume = 32;
  1033.        audio_req->ioa_Period = 3546895 / 880 / SINE_DATA_LENGTH;
  1034.  
  1035. Once all d3esired values have been set you send the request to
  1036. the audio device, by calling the BeginIO() function. If you
  1037. want to wait for the sound to be completed you can use the
  1038. WaitIO() function. Note that you should NEVER try to wait for
  1039. a request that has not been started, or if the "ioa_Cycles"
  1040. field has been set to 0 (forever). If the request already has
  1041. been completed the WaitIO() function will immediately return.
  1042.  
  1043.     BeginIO( audio_req );
  1044.  
  1045.     error = WaitIO( audio_req );
  1046.  
  1047.     if( error )
  1048.       clean_up( "Error while playing the sound!" );
  1049.  
  1050.  
  1051.  
  1052. 4.3.2  USE SEVERAL REQUEST BLOCKS
  1053.  
  1054. Remember that you may not modify the request block once it has
  1055. been sent, by calling BeginIO(), and have not yet been
  1056. completed. Once the request has been completed you of course
  1057. start to modify it again. If you want to play several notes at
  1058. the same times in different sound channels, you may therefore
  1059. have to use several request blocks.
  1060.  
  1061. If you want to use some of the audio commands that is changing
  1062. the sound which is currently being played you also have to a
  1063. use separate request blocks. In many programs you will see
  1064. four request blocks where each one handles one audio channel.
  1065. Very often there also exist one or more extra request blocks
  1066. that are used to modify sounds which are currently being
  1067. played.
  1068.  
  1069. If you have reserved a sound channel with a request block you
  1070. can not use another request block to play sounds on it if you
  1071. do not first copy some important values to the new request
  1072. block.
  1073.  
  1074. When you reserve a channel the request block is given a unique
  1075. key value, and if this key value does not match with the sound
  1076. channel's current key value the request will fail. Before you
  1077. may use a new request block you must therefore first copy the
  1078. "ioa_AllocKey" value to the new request.
  1079.  
  1080. You must also copy the pointer to the audio device from the
  1081. old request to the new one. The unit number and all stuff in
  1082. the Message structure must also be copied. In the end, it is
  1083. actually easier to copy the whole request block, even if some
  1084. fields does not have to be copied.
  1085.  
  1086. Here is a small demonstration on how to copy all values in a
  1087. request block to a new one. The "audio_req" is a pointer to an
  1088. already initialized audio request block, where "change_req" is
  1089. a new and uninitialized request block.
  1090.  
  1091.   /* Source and destination pointers: */
  1092.   BYTE *first_ptr;
  1093.   BYTE *second_ptr;
  1094.  
  1095.  
  1096.   /* Allocate request blocks, open audio device, */
  1097.   /* reserve channels, etc...                    */
  1098.  
  1099.  
  1100.   /* Copy the first request block to the */
  1101.   /* second one: (byte by byte)          */
  1102.  
  1103.   /* Get the start addresses of both request blocks: */
  1104.   first_ptr = (BYTE *) audio_req;
  1105.   second_ptr = (BYTE *) change_req;
  1106.  
  1107.   /* Copy byte by byte: */
  1108.   for( loop = 0; loop < sizeof( struct IOAudio ); loop++ )
  1109.   {
  1110.     /* Copy: */
  1111.     *second_ptr = *first_ptr;
  1112.     
  1113.     /* Next byte: */
  1114.     first_ptr++;
  1115.     second_ptr++;
  1116.   }
  1117.  
  1118.  
  1119.  
  1120. 4.3.2  PLAY DOUBLE BUFFERED SOUNDS
  1121.  
  1122. If you play several sounds after each other you will hear a
  1123. "click" between the sounds. The reason why you hear this click
  1124. is that once a sound has been completed it takes some time for
  1125. your program to initialize a new request and send it to the
  1126. audio device. Although this delay is very short it is long
  1127. enough to be noticed, especially if several programs are
  1128. running at the same time.
  1129.  
  1130. Luckily there is a solution for this problem. Before the first
  1131. sound has been complete you should send the next sound request.
  1132. When the first sound has stopped the second sound is already
  1133. available for the audio device, and it will immediately be
  1134. played. Because of this, there is such a short interrupt
  1135. between the sounds that you will not hear any annoying clicks.
  1136.  
  1137. This technique of sending the next sound when the first one 
  1138. is still being played is usually called "double buffering",
  1139. and can in many ways be compared with "double buffered
  1140. displays" which is described in the manual "Graphics",
  1141. chapter "Graphical Tricks".
  1142.  
  1143.   1. Prepare the first request block and send it to the audio
  1144.      device.
  1145.  
  1146.   2. Prepare the second request block, and send it to the audio
  1147.      device.
  1148.  
  1149.   3. Wait for the first request block to be completed. When the
  1150.      request block is returned you prepare it again for a new
  1151.      sound, and send it to the audio device.
  1152.  
  1153.   4. Wait for the second request block to be completed. When
  1154.      the request block is returned you prepare it again for a
  1155.      new sound, and send it to the audio device.
  1156.  
  1157.   5. Jump back to step 3 until all sounds have been played.
  1158.  
  1159.   6. Wait for both requests to be completed, and quit.
  1160.  
  1161.  
  1162.  
  1163. 4.3.3  MODIFY THE HARDWARE REGISTERS
  1164.  
  1165. Usually you should never try to modify hardware registers
  1166. directly but instead use special functions that does it for
  1167. you. However, when you are using the audio device you may alter
  1168. the hardware registers of the sound channels. Modifying
  1169. hardware registers directly is much faster than using functions
  1170. that does it for you, and when playing sounds speed can
  1171. sometimes be crucial.
  1172.  
  1173. Before you may alter any of the sound registers you must first
  1174. lock the channel(s)! When you modify hardware registers no one
  1175. is no checking that you really own the channel(s). If you have
  1176. reserved a channel and not locked it, some other program may
  1177. steal it and you would not notice that. You could then end up
  1178. altering the other programs sound channel(s), which is rather
  1179. unpolite not to say dangerous.
  1180.  
  1181. To use the hardware registers you have to declare an external
  1182. array of "AudChannel" structures. The AudChannel structure is
  1183. defined in the Custom structure which is declared in the header
  1184. file "hardware/custom.h". The whole Custom structure is
  1185. automatically initialized by the Amiga. The AudChannel array,
  1186. which must be called "aud" is also initialized.
  1187.  
  1188. The AudChannel structure is defined like this: (Declared in
  1189. header file "hardware/custom.h")
  1190.  
  1191.   struct AudChannel
  1192.   {
  1193.     UWORD *ac_ptr;
  1194.     UWORD ac_len;
  1195.     UWORD ac_per;
  1196.     UWORD ac_vol;
  1197.     UWORD ac_dat;
  1198.     UWORD ac_pad[2];
  1199.   } aud[4];
  1200.  
  1201.   ac_ptr:    Pointer to the waveform data.
  1202.  
  1203.   ac_len:    The length of the waveform.
  1204.  
  1205.   ac_per:    The period value.
  1206.  
  1207.   ac_vol:    The volume.
  1208.  
  1209.   ac_dat:    This field contains the first two bytes which will
  1210.              be played. The DMA channels will automatically
  1211.              move data from the waveform to this field, two
  1212.              bytes each time, when needed. You can alter these
  1213.              values directly, but this will take longer time
  1214.              than letting the DMA fetch the sound
  1215.              automatically, so it is not recommended.
  1216.  
  1217.   ac_pad[2]: Unused areas, should not be used.
  1218.  
  1219.  
  1220. When you declare the "aud" array you probably have to use the
  1221. keyword "far" since the hardware data is most certainly far
  1222. away from your program. Remember that the aud array has been
  1223. declared and initialized by the Amiga itself, and you should
  1224. therefore declare it as an external array. Example:
  1225.  
  1226.   extern struct AudChannel far aud[];
  1227.  
  1228.  
  1229. To use the hardware audio channels you must also declare an
  1230. external UWORD which must be called "dmacon". This variable
  1231. is also automatically initialized by the Amiga. Example:
  1232.  
  1233.   extern UWORD far dmacon;
  1234.  
  1235.  
  1236. This "dmacon" variable controls the DMA channels. A DMA channel
  1237. can be described as a data cable where data can be sent without
  1238. disturbing the main processor. It is because of the DMA
  1239. (DMA = "Direct Memory Access") you can play sounds at the same
  1240. time as your program can use the main processor to do something
  1241. else.
  1242.  
  1243. It is this variable which controls all "DMA writing", and must
  1244. therefore be used when you want to play a sound. To play a
  1245. sound on one of the audio channels you have to give it a value
  1246. where the leftmost bit is set (the DMAF_SETCLR constant) and
  1247. the bit which correspond to the audio channel you want to start
  1248. playing. Bit one represents the first audio channel, bit two
  1249. the second audio channel and so on. You should however not use
  1250. the direct values but instead the constants which are declared
  1251. in the "hardware/dmabits.h" header file.
  1252.  
  1253.   DMAF_SETCLR  This flag should always be set together with the
  1254.                channel(s) you want to be played.
  1255.   
  1256.   DMAF_AUD0    First left audio channel.
  1257.  
  1258.   DMAF_AUD1    First right audio channel.
  1259.  
  1260.   DMAF_AUD2    Second right audio channel.
  1261.  
  1262.   DMAF_AUD3    Second left audio channel.
  1263.  
  1264. If you want to use several audio channels you simple set all
  1265. desired channel flags separated with the binary OR operator "|".
  1266. To start all audio channels do like this: (You must of course
  1267. first have reserved and locked them before you may do this!)
  1268.  
  1269.   dmacon = DMAF_SETCLR|DMAF_AUD0|DMAF_AUD1|DMAF_AUD2|DMAF_AUD3;
  1270.  
  1271.  
  1272. Since you can now directly alter the volume, period, and
  1273. waveform by directly writing to the hardware the changes will
  1274. be very simple and quick. Just remember to now and then check
  1275. if your lock has been returned, and if so clean up after you
  1276. and free the channel(s) as soon as possible. Do not be greedy
  1277. and ignore the lock, too many programs do that which is very
  1278. irritating!
  1279.  
  1280.  
  1281.  
  1282. 4.4  CLEAN UP AFTERWARDS
  1283.  
  1284. When your program terminates you have to clean up after
  1285. yourself, as usual. It is important that you do not forget to
  1286. do this since other programs are denied any access to the
  1287. audio channels and memory you forget to deallocate. Remember
  1288. that your programs must also be able to clean up after itself
  1289. if it suddenly has to terminate because of some error.
  1290.  
  1291.  
  1292.  
  1293. 4.4.1  UNLOCK CHANNELS
  1294.  
  1295. All audio channels that you have reserved must be given back to
  1296. the system before your program may terminate. If you have
  1297. locked a channel and another programs wants it, you must return
  1298. it as soon as possible. However, if you have not locked the
  1299. channel and another program has stolen it you do not have to
  1300. free it.
  1301.  
  1302. To free one or more audio channels you simply send a request
  1303. with the "ADCMD_FREE" command flag set. All channels which are
  1304. selected in the "io_Unit" field of the request block will be
  1305. resetted and given back to the system. Any locks on the
  1306. channels will be removed.
  1307.  
  1308. If some other program has stolen the channel(s) from you the
  1309. request will be returned with the error message 
  1310. "ADIOERR_NOALLOCATION". You do not have to do anything with
  1311. channel(s) which has/have been stolen from you. It is the other
  1312. program's duty to clean it/them up.
  1313.  
  1314. When you want to free a channel you may use the DoIO() 
  1315. function, instead of BeginIO(). Normally you should only use
  1316. the BeginIO() function when working with the audio device, but
  1317. when you free channel(s) the DoIO() will work equally well.
  1318.  
  1319.     audio_req->ioa_Request.io_Command = ADCMD_FREE;
  1320.  
  1321.     DoIO( audio_req );
  1322.  
  1323.  
  1324.  
  1325. 4.4.2  REMOVE ALL MESSAGES AND CLOSE THE REPLY PORT
  1326.  
  1327. You must close all message ports you have opened. Just remember
  1328. to remove all messages before you close it. A short while loop
  1329. that removes the messages until it can not find any more is
  1330. usually enough.
  1331.  
  1332.   while( GetMsg( replymp ) )
  1333.     printf( "Collected a message at the reply port.\n" );
  1334.  
  1335.   DeletePort( replymp);
  1336.   
  1337.  
  1338.  
  1339. 4.4.3  CLOSE THE DEVICE
  1340.  
  1341. The audio device itself must of course also be closed if you
  1342. have opened it. If you have open it several times with
  1343. different request blocks you have to close all of them.
  1344.  
  1345.   CloseDevice( audio_req );
  1346.  
  1347.  
  1348. 4.4.4  DEALLOCATE THE REQUEST BLOCKS
  1349.  
  1350. All audio request blocks you have created must be deallocated
  1351. with help of the DeleteExtIO() function. The size should be set
  1352. to the size of an IOAudio structure. Just remember to close the
  1353. device before you deallocate the request block.
  1354.  
  1355.   DeleteExtIO( audio_req, sizeof( struct IOAudio ) );
  1356.  
  1357.  
  1358.  
  1359. 4.4.5  DEALLOCATE SOUND BUFFERS
  1360.  
  1361. If you have allocated a sound buffer to store the waveform in
  1362. you must also deallocate it. Remember to deallocate the same
  1363. amount of memory as when you allocated it.
  1364.  
  1365.   FreeMem( square_wave, SINE_DATA_LENGTH );
  1366.  
  1367.  
  1368.  
  1369. 4.5  AUDIO DEVICE COMMANDS
  1370.  
  1371. The audio device is controlled by set of commands. We have
  1372. looked at the ADCMD_ALLOCATE, ADCMD_FREE, and CMD_WRITE
  1373. commands. Most things can be managed with these commands, but
  1374. there exist a set of other commands that may sometimes be
  1375. needed. These new commands can be divided into two groups. The
  1376. first group consists of the general device commands which can
  1377. be used on most devices. The second group consists of a set
  1378. special commands that only the audio device can understand
  1379. and handle.
  1380.  
  1381. 4.5.1  GENERAL DEVICE COMMANDS
  1382.  
  1383. Here is the complete list of general device commands, together
  1384. with a short description:
  1385.  
  1386.   CMD_WRITE  Start to play a sound on one channel.
  1387.   CMD_READ   Check which request is currently playing.
  1388.   CMD_STOP   Temporarily stop all audio requests.
  1389.   CMD_START  Restart the previously stopped audio requests.
  1390.   CMD_FLUSH  Remove all queued requests.
  1391.   CMD_RESET  Reset the audio device.
  1392.  
  1393.  
  1394. 4.5.1.1  CMD_WRITE
  1395.  
  1396. The CMD_WRITE command is used when you want to start to play
  1397. a sound on a specific sound channel. Note that it can only
  1398. start one sound channel.
  1399.  
  1400. This command has already been explained, so see previous
  1401. sections for more information.
  1402.  
  1403.  
  1404.  
  1405. 4.5.1.2  CMD_READ
  1406.  
  1407. The CMD_READ command is used when you want to get the address
  1408. of the request which is currently being used. You specify a
  1409. sound channel you want to check by setting the corresponding
  1410. bit in the io_Unit field. If a sound is currently being played
  1411. on that channel the "io_Data" field of our request block will
  1412. contain the address of the other request which is currently
  1413. playing the sound. If no sound is being played the "io_Data"
  1414. field is set to NULL.
  1415.  
  1416. This command is useful if you have sent a lot of requests to
  1417. the audio device and you want to see which request is currently
  1418. being played. Note that you must copy the sound channels key
  1419. value to your request before you can check it. The key value
  1420. can be found in the "ioa_AllocKey" field of the request which
  1421. reserved the channel. If your request block's "ioa_AllocKey"
  1422. value does not match the sound channel's current key value the
  1423. ADIOERR_NOALLOCATION error message is returned.
  1424.  
  1425.  
  1426.  
  1427. 4.5.1.3  CMD_STOP
  1428.  
  1429. The CMD_STOP command will temporarily stop all requests for the
  1430. specified channel(s). All requests will be queued, and first
  1431. processed when you later restart the channel(s) by using the
  1432. CMD_START command. The "io_Unit" field of the request specifies
  1433. which channels that should be stopped. Remember to copy the
  1434. key value from the request block that reserved the sound
  1435. channel(s). If your request block's "ioa_AllocKey" value does
  1436. not match the sound channel's current key value the
  1437. ADIOERR_NOALLOCATION error message is returned.
  1438.  
  1439.  
  1440.  
  1441. 4.5.1.4  CMD_START
  1442.  
  1443. The CMD_START command should be used when you want to restart
  1444. one or more previously stopped sound channel(s). The "io_Unit"
  1445. field of the request specifies which channels that should be
  1446. restarted. Remember to copy the key value from the request
  1447. block that reserved the sound channel(s). If your request
  1448. block's "ioa_AllocKey" value does not match the sound channel's
  1449. current key value the ADIOERR_NOALLOCATION error message is
  1450. returned.
  1451.  
  1452.  
  1453.  
  1454. 4.5.1.5  CMD_FLUSH
  1455.  
  1456. The CMD_FLUSH command will remove all currently queued commands
  1457. from the specified channels. Remember to copy the key value
  1458. from the request block that reserved the sound channel(s). If
  1459. your request block's "ioa_AllocKey" value does not match the
  1460. sound channel's current key value the ADIOERR_NOALLOCATION
  1461. error message is returned.
  1462.  
  1463.  
  1464.  
  1465. 4.5.1.6  CMD_RESET
  1466.  
  1467. The CMD_RESET command will reset all specified channels. All
  1468. requests currently queued will be removed, the audio hardware
  1469. registers are cleared, and if the channel has been stopped it
  1470. is restarted. Remember to copy the key value from the request
  1471. block that reserved the sound channel(s). If your request
  1472. block's "ioa_AllocKey" value does not match the sound channel's
  1473. current key value the ADIOERR_NOALLOCATION error message is
  1474. returned.
  1475.  
  1476.  
  1477.  
  1478. 4.5.2  SPECIAL AUDIO DEVICE COMMANDS
  1479.  
  1480. Here is the complete list of special audio device commands.
  1481. These commands can only be used with the audio device.
  1482.  
  1483.   ADCMD_ALLOCATE   Tries to reserve one or more audio channels.
  1484.   ADCMD_FREE       Frees one or more audio channels.
  1485.   ADCMD_SETPREC    Change the priority of a sound.
  1486.   ADCMD_FINISH     Finish (aborts) a sound.
  1487.   ADCMD_PERVOL     Change the volume and period of a sound.
  1488.   ADCMD_LOCK       Will lock audio one or more channel(s).
  1489.   ADCMD_WAITCYCLE  Waits for a cycle to be completed.
  1490.  
  1491.  
  1492.  
  1493. 4.5.1.1  ADCMD_ALLOCATE
  1494.  
  1495. The ADCMD_ALLOCATE command will reserve one or more audio
  1496. channels. This command has already been discussed, so see
  1497. previous sections for more information about this command.
  1498.  
  1499.  
  1500.  
  1501. 4.5.1.2  ADCMD_FREE
  1502.  
  1503. The ADCMD_FREE command will free one or more audio channels
  1504. that has(have) previously been stolen. This command has already
  1505. been discussed, so see previous sections for more information
  1506. about this command.
  1507.  
  1508.  
  1509.  
  1510. 4.5.1.3  ADCMD_SETPREC
  1511.  
  1512. The ADCMD_SETPREC command allows you to change the priority
  1513. of a sound channel that has previously been reserved. You may
  1514. for example want to use a very high priority at the beginning
  1515. of the sound, and later decrease it. Remember to copy the key
  1516. value from the request block that reserved the sound channel(s).
  1517. If your request block's "ioa_AllocKey" value does not match the
  1518. sound channel's current key value the ADIOERR_NOALLOCATION
  1519. error message is returned.
  1520.  
  1521.  
  1522.  
  1523. 4.5.1.4  ADCMD_FINISH
  1524.  
  1525. The ADCMD_FINISH command will finish an already started sound.
  1526. This may be needed if you have set a very high cycle value, or
  1527. you are playing a sound "for ever" (the ioa_Cycle field set to
  1528. zero.) This command has the same effect as calling the
  1529. AbortIO() function. Both options are equally good, but the
  1530. ADCMD_COMMAND looks a bit "cleaner" (at least some C
  1531. programmers think so).
  1532.  
  1533. Remember to copy the key value from the request block that
  1534. reserved the sound channel(s). If your request block's
  1535. "ioa_AllocKey" value does not match the sound channel's current
  1536. key value the ADIOERR_NOALLOCATION error message is returned.
  1537.  
  1538.  
  1539.  
  1540. 4.5.1.5  ADCMD_PERVOL
  1541.  
  1542. The ADCMD_PERVOL command can be useful if you want to change
  1543. the period and/or volume value(s) of a sound which is already
  1544. playing. Just remember to set the ADIOF_PERVOL flag in the
  1545. "io_Flags" field, or else your command will have no effect.
  1546. Remember to also set the new volume and period values you want
  1547. to be used in the "ioa_Volume" and "ioa_Period" fields.
  1548.  
  1549. As always, remember to copy the key value from the request
  1550. block that reserved the sound channel(s). If your request
  1551. block's "ioa_AllocKey" value does not match the sound channel's
  1552. current key value the ADIOERR_NOALLOCATION error message is
  1553. returned.
  1554.  
  1555.  
  1556.  
  1557. 4.5.1.6  ADCMD_LOCK
  1558.  
  1559. The ADCMD_LOCK command is used to lock one or more channels.
  1560. If some other program has higher priority and tries to steal
  1561. our channel(s), the lock is returned to us. The channels are
  1562. however still not stolen. When the lock is returned we should
  1563. as fast as possible clean up and free the channels so the other
  1564. program can get it.
  1565.  
  1566. See the previous sections for more information about this
  1567. command.
  1568.  
  1569.  
  1570.  
  1571. 4.5.1.7  ADCMD_WAITCYCLE
  1572.  
  1573. If you send a request with the "ADCMD_WAITCYCLE" command set,
  1574. the request will first be sent back when the sound which is
  1575. currently being played has reached the end of the cycle. If
  1576. no sound is being played it will immediately return.
  1577.  
  1578. Remember to copy the key value from the request block that
  1579. reserved the sound channel(s). If your request block's
  1580. "ioa_AllocKey" value does not match the sound channel's current
  1581. key value the ADIOERR_NOALLOCATION error message is returned.
  1582.  
  1583. 4.6  FUNCTIONS
  1584.  
  1585.  
  1586. BeginIO()
  1587.  
  1588.   BeginIO() is a low level form of the SendIO() function. The
  1589.   advantage with BeginIO() is that no fields of the request
  1590.   block will be altered as which is the case with SendIO().
  1591.   When you are using the audio device you should almost always
  1592.   use the BeginIO() and not DoIO() or SendIO(). If you are
  1593.   freeing channels you may however use these functions too.
  1594.  
  1595.   Synopsis: BeginIO( req )
  1596.  
  1597.   req:      (struct IORequest *) Pointer to the request you
  1598.             want to have executed. In this case a pointer to
  1599.             an IOAudio structure.
  1600.  
  1601.  
  1602. DoIO()
  1603.  
  1604.   DoIO() is used to send requests to a device, and waits for it
  1605.   to be completed. While the program is waiting it is put to
  1606.   sleep so it will not waste any computer time. DoIO() will
  1607.   return first when the request have been completed or failed,
  1608.   and no message is therefore sent to the reply port. When you
  1609.   are using the audio device you may only use this function
  1610.   when you free audio channel(s). 
  1611.  
  1612.   Synopsis: error = DoIO( req );
  1613.  
  1614.   error:    (long) DoIO() will return first when the request has
  1615.             been completed or something has failed. If the
  1616.             request was successfully completed zero is returned,
  1617.             else an error number is returned. What error number
  1618.             depends on which device was used.
  1619.  
  1620.   req:      (struct IORequest *) Pointer to the request you
  1621.             want to have executed. In this case a pointer to
  1622.             an IOAudio structure.
  1623.  
  1624.  
  1625.  
  1626. SendIO()
  1627.  
  1628.   SendIO() is used to send requests to a device, but will
  1629.   return immediately without any delay. To check if the request
  1630.   have been completed use the CheckIO() function, or look
  1631.   at the request's reply port for any messages. Once the
  1632.   request has been completed you must remove the message at
  1633.   the reply port. (CheckIO() will not do it.) To remove a
  1634.   message use the function Remove(). Note that you may NOT
  1635.   close the device before all requests have been completed or
  1636.   aborted!
  1637.   
  1638.   When you are using the audio device you may only use this
  1639.   function when you free audio channel(s). 
  1640.  
  1641.   Synopsis: SendIO( req )
  1642.  
  1643.   req:      (struct IORequest *) Pointer to the request you
  1644.             want to have executed. In this case a pointer to
  1645.             an IOAudio structure.
  1646.  
  1647.  
  1648.  
  1649. CheckIO()
  1650.  
  1651.   CheckIO() is used to check if a previously started request
  1652.   has been completed. Note that this function will not remove
  1653.   the message at the reply port. This must be done with the
  1654.   Remove() function.
  1655.  
  1656.   Synopsis: ptr = CheckIO( req );
  1657.  
  1658.   ptr:      (long) CheckIO() will either return NULL if the
  1659.             request have not been completed or it will return a
  1660.             pointer to the request block.
  1661.  
  1662.   req:      (struct IORequest *) Pointer to the request you
  1663.             want to check. In this case a pointer to
  1664.             an IOAudio structure.
  1665.  
  1666.  
  1667.  
  1668.  
  1669. WaitIO()
  1670.  
  1671.   WaitIO() will wait for the request to be completed, and while
  1672.   the program is waiting it is put to sleep so no computer time
  1673.   is wasted.
  1674.  
  1675.   Synopsis: error = WaitIO( req );
  1676.  
  1677.   error:    (long) WaitIO() will return first when the request,
  1678.             that has previously been sent, has been completed or
  1679.             something has failed. If the request was successfully
  1680.             completed zero is returned, else an error number is
  1681.             returned. What error number depends on which device
  1682.             was used.
  1683.  
  1684.   req:      (struct IORequest *) Pointer to the request you
  1685.             want to wait for to be completed.  In this case a
  1686.             pointer to an IOAudio structure. Note that the
  1687.             request must have already been sent to the device
  1688.             by either a SendIO() or BeginIO() function call.
  1689.  
  1690.  
  1691. AbortIO()
  1692.  
  1693.   AbortIO() will try to abort a previously started request.
  1694.   Instead of using this function you can use the ADCMD_FINISH
  1695.   command as explained above.
  1696.   
  1697.   A request that is aborted will have its io_Error field set
  1698.   to IOERR_ABORTED (defined in header file "exec/errors.h").
  1699.  
  1700.   Synopsis: AbortIO( req )
  1701.  
  1702.   req:      (struct IORequest *) Pointer to the request you
  1703.             want to abort. In this case a pointer to an IOAudio
  1704.             structure.
  1705.  
  1706.  
  1707.  
  1708. CloseDevice()
  1709.  
  1710.   CloseDevice() will close a device. Note that you should NOT
  1711.   close the device before all started asynchronous requests
  1712.   have either been completed or aborted.
  1713.   
  1714.   Synopsis: CloseDevice( ioreq );
  1715.  
  1716.   ioreg:    (struct IORequest *) Pointer to the device's
  1717.             request block. In this case a pointer to an IOAudio
  1718.             structure.
  1719.  
  1720.  
  1721.  
  1722. OpenDevice()
  1723.  
  1724.   OpenDevice() will try to open the specified device.
  1725.   
  1726.   Synopsis: error = OpenDevice( name, unit, req, flags );
  1727.  
  1728.   error:    (long) If OpenDevice() managed to open the device
  1729.             it returns 0, else an error number is returned.
  1730.             If you try to reserve one or more audio channels
  1731.             at the same time, and there are not any available,
  1732.             the error message "IOERR_ALLOCFAILED" is returned.
  1733.  
  1734.   name:     (char *) Name of the device you want to open.
  1735.             The name of the audio device is defined as
  1736.             AUDIONAME in header file "audio.device".
  1737.  
  1738.   unit:     (long) Not used by the audio device.
  1739.  
  1740.   req:      (struct IORequest *) Pointer to a request block.
  1741.             In this case a pointer to an IOAudio structure.
  1742.  
  1743.   flags:    (long) Not used by the Audio device.
  1744.  
  1745.  
  1746.  
  1747. 4.7  EXAMPLES
  1748.  
  1749. Example 1
  1750.   This program will play some notes (A to G#) with help of the
  1751.   Audio Device. It will use one of the audio channels (the
  1752.   first one which is free).
  1753.  
  1754. Example 2
  1755.   This program is very similar to the previous example, but
  1756.   this time are we using double buffered sounds, and there will
  1757.   therefore not be any annoying "clicks" between the notes.
  1758.  
  1759.   The technique with double buffered sound is that while the
  1760.   first sound is played the second sound is already sent to
  1761.   the audio device. When the first sound terminates the second
  1762.   sound can immediately start without any delay. While the
  1763.   second sound is being played the first sound is prepared and
  1764.   sent and so on... Because there is never any delay between
  1765.   the sounds there will never be any annoying clicks.
  1766.  
  1767. Example 3
  1768.   This program demonstrates how you can play some sampled data.
  1769.   Remember that sampled data is just a more complicated
  1770.   waveform.
  1771.  
  1772.   The sampled sound has been converted into numbers by
  1773.   "PrintSound", a utility included in the "Sound" manual.
  1774.  
  1775. Example 4
  1776.   This program demonstrates how you can play a sound
  1777.   continuously. While the sound is being played we slowly alter
  1778.   the period and volume values.
  1779.  
  1780. Example 5
  1781.   This example demonstrates how you can play sounds in STEREO.
  1782.   First we play a sound in the left channel, then we switch to
  1783.   the right, and then back again, and so on...
  1784.  
  1785.   In this example are we reserving the audio channels at the
  1786.   same time as we open the audio device. We use two audio
  1787.   requests, one for the left channel and the other one for the
  1788.   right channel.
  1789.  
  1790. Example 6
  1791.   This program will play some notes (A to G#) with help of the
  1792.   Audio Device. It will use as many audio channels as possible,
  1793.   and we are modifying the hardware registers directly instead
  1794.   of using the special Audio Device commands.
  1795.  
  1796.   You are allowed to use the hardware registers directly if you
  1797.   make sure that no other task can steel them from you before
  1798.   you have cleared all necessary registers.
  1799.  
  1800.